// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_ui_manager.h"
#include "mk_error.h"
#include "mk_engine.h"

namespace MK3DE
{
    CUIManager::CUIManager(CUIRenderer* renderer) : 
        _renderer(renderer)
    {

    }
    

    CUIManager::~CUIManager()
    {
        std::vector<CUIWindow*>::iterator i = _vecWindow.begin();
        while (i != _vecWindow.end())
        {
            CUIWindow* win = *i;
            SAFE_DELETE(win);
            ++i;
        }        

        std::vector<CFTFont*>::iterator fi = _vecFont.begin();
        while (fi != _vecFont.end())
        {
            CFTFont* font = *fi;
            SAFE_RELEASE(font);
            ++fi;
        }
    }

    BOOL CUIManager::Update(DWORD dt)
    {
        std::vector<CUIWindow*>::iterator i = _vecWindow.begin();
        while (i != _vecWindow.end())
        {
            CUIWindow* win = *i;
            win->Update(dt);
            ++i;
        }

        return TRUE;
    }

    BOOL CUIManager::Render()
    {
        std::vector<CUIWindow*>::iterator i = _vecWindow.begin();
        while (i != _vecWindow.end())
        {
            CUIWindow* win = *i;
            win->Render();
            ++i;
        }

        _renderer->FlushRender();

        return TRUE;
    }

    CUIStatic* CUIManager::AddStatic(CUIWindow* parent, D3DXVECTOR2& pos, D3DXVECTOR2& size, const char* name)
    {
        CUIStatic* control = new CUIStatic(_renderer, parent, pos, size, name);
        MK_PROCESS_ERROR(control);

        if (parent == NULL)
            _vecWindow.push_back(control);
        else
            parent->AddChild((CUIWindow*)control);

        return control;
Exit0:
        return NULL;
    }

    CUIButton* CUIManager::AddButton(CUIWindow* parent, D3DXVECTOR2& pos, D3DXVECTOR2& size, const char* name)
    {
        CUIButton* control = new CUIButton(_renderer, parent, pos, size, name);
        MK_PROCESS_ERROR(control);

        if (parent == NULL)
            _vecWindow.push_back(control);
        else
            parent->AddChild((CUIWindow*)control);

        return control;
Exit0:
        return NULL;
    }

    CUIEditBox* CUIManager::AddEditBox(CUIWindow* parent, D3DXVECTOR2& pos, D3DXVECTOR2& size, const char* name)
    {
        CUIEditBox* control = new CUIEditBox(_renderer, parent, pos, size, name);
        MK_PROCESS_ERROR(control);

        if (parent == NULL)
            _vecWindow.push_back(control);
        else
            parent->AddChild((CUIWindow*)control);

        return control;
Exit0:
        return NULL;
    }

    int CUIManager::AddFont(const char* fileName, BOOL bAntialiased, DWORD points)
    {
        CFTFont* font;
        BOOL ret = GetEngine()->GetResourceManager()->GetFTFont(fileName, bAntialiased, points, font);
        MK_PROCESS_ERROR(ret);

        _vecFont.push_back(font);

        ret = TRUE;
Exit0:
        if (ret)
            return (int)_vecFont.size() - 1;
        else
            return -1;
    }

    CFTFont* CUIManager::GetFont(int index)
    {
        if (index >= 0 && index < (int)_vecFont.size())
            return _vecFont[index];
        return NULL;
    }

    BOOL CUIManager::MsgProc(UIInputEvent& event)
    {
        CUIWindow* focus = CUIWindow::GetFocus();
        if (focus && focus->MsgProc(event))
            return TRUE;

        std::vector<CUIWindow*>::iterator i = _vecWindow.begin();
        while (i != _vecWindow.end())
        {
            CUIWindow* win = *i;
            if (win->MsgProc(event))
                return TRUE;

            ++i;
        }

        return FALSE;
    }

    void CUIManager::PushUIEvent(UIEvent& event)
    {
        _uiEventQueue.push_back(event);
    }

    BOOL CUIManager::PopUIEvent(UIEvent& event)
    {
        if (_uiEventQueue.size() == 0)
            return FALSE;

        event = _uiEventQueue.front();
        _uiEventQueue.pop_front();
        return TRUE;
    }
}